package com.wanchuang.hepos.ui.page.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.wanchuang.architecture.utils.APKVersionCodeUtils;
import com.wanchuang.architecture.utils.CacheUtil;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.repository.WebApis;
import com.wanchuang.hepos.bridge.request.MineRequestViewModel;
import com.wanchuang.hepos.bridge.state.mine.MineSettingViewModel;
import com.wanchuang.hepos.databinding.FragmentMineSettingBinding;
import com.wanchuang.hepos.help.DialogUtils;
import com.wanchuang.hepos.help.UserHelper;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.LoginActivity;
import com.wanchuang.hepos.ui.web.ui.WebViewActivity;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class MineSettingFragment extends BaseFragment {
    private FragmentMineSettingBinding mBinding;
    private MineSettingViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MineSettingViewModel.class);
        mViewModel.cash.set(CacheUtil.getCacheSize(mActivity));
        mViewModel.banben.set("当前版本 " + APKVersionCodeUtils.getVerName(mActivity));
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine_setting, container, false);
        mBinding = FragmentMineSettingBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void clear() {
            CacheUtil.cleanAllCache(mActivity);
            mViewModel.cash.set(CacheUtil.getCacheSize(mActivity));
        }

        public void changePassword() {
            nav().navigate(R.id.action_mineSettingFragment_to_changePasswordFragment);
        }

        public void settingJiesuan() {
            WebViewActivity.startActivity(mActivity, WebApis.settlementMode);
        }

        public void loginOut() {
            DialogUtils.showDefaultDialog(mActivity, "", "确定退出登录?", new DialogUtils.DialogImpl() {
                @Override
                public void onOk() {
                    NetworkManager.INSTANCE.post(Apis.logout, new OnRequestCallBack() {
                        @Override
                        public void onOk(byte[] bytes) {
                        }

                        @Override
                        public void onError(int code, String errorMessage) {
                        }
                    });
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivity(intent);
                    UserHelper.loginOut();
                    mActivity.finish();
                }
            });
        }

    }
}
